<?php

declare(strict_types=1);

namespace App\Services;

use Illuminate\Support\Facades\Log;

class Service
{
    //需要子类定义
    private static $instance;
    private static $appId = '';

    protected function __construct()
    {
    }

    /**
     * @return \App\Services\Service
     */
    public static function getInstance()
    {
        if (static::$instance === null) {
            static::$instance = new static();
        }

        return static::$instance;
    }

    public static function result($data = '', $msg = '请求成功', $status = 0)
    {
        if (empty($data)) {
            $data = [];
        }

        return [
            'data' => $data,
            'msg'  => $msg,
            'code' => $status,
        ];
    }

    public static function formatResult($result = [])
    {
        if (empty($result)) {
            return ['code' => 1, 'msg' => '执行失败', 'data' => []];
        }
        if (isset($result['data']) && !$result['data']) {
            $result['data'] = [];
        }
        if (!isset($result['code'])) {
            //不是SDK返回的标准格式，部分业务不需要访问后端接口
            return ['code' => 0, 'msg' => '执行成功', 'data' => $result];
        }

        return ['code' => $result['code'], 'msg' => $result['msg'], 'data' => $result['data']];
    }

    /**
     * 生成手机验证码，一天内保持不变
     *
     * @param $mobile
     *
     * @return int
     */
    public static function genSuperMobileCaptcha($mobile)
    {
        Log::info(__CLASS__ . '.genMobileCaptcha' . $mobile);
        $captcha = $mobile % date('Ymd');
        $captcha = substr($captcha, 0, 4);
        Log::info(__CLASS__ . '.genMobileCaptcha ret ' . $captcha);

        return $captcha;
    }
}
